package com.aaa.vip.controller;

import com.aaa.common.constant.ConstantUtil;
import com.aaa.common.entity.FrontUser;
import com.aaa.common.util.BaseUtil;
import com.aaa.common.util.OssUtil;
import com.aaa.common.util.RedisUtil;
import com.aaa.common.util.Result;
import com.aaa.common.vo.Page;
import com.aaa.common.vo.UserRegisterStatisticsVO;
import com.aaa.vip.service.FrontUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * (FrontUser)表控制层
 *
 * @author makejava
 * @since 2025-09-20 09:05:34
 */
@RestController
@RequestMapping("frontUser")
@Log4j2
public class FrontUserController {
    /**
     * 服务对象
     */
    @Resource
    private FrontUserService frontUserService;

    @GetMapping("updatePassword")
    public Result updatePassword(String phone, String password) {
        return BaseUtil.success(frontUserService.updatePassword(phone, password));
    }

    @PostMapping("uploadPic")
    public Result uploadPic(@RequestParam("pic") MultipartFile file){
        //调用工具类，进行上传
        return BaseUtil.success(OssUtil.uploadFile("user/images",file));
    }

    @GetMapping("createCode")
    public Result createCode(String phoneNumber) {
        RedisUtil.init(ConstantUtil.RedisConstant.REDIS_HOST, ConstantUtil.RedisConstant.REDIS_PORT, ConstantUtil.RedisConstant.REDIS_PASSWORD);
        return BaseUtil.success(RedisUtil.setData(phoneNumber, RedisUtil.generateVerifyCode()));
    }

    @GetMapping("checkCode")
    public Result checkCode(String phoneNumber, String number) {
        RedisUtil.init(ConstantUtil.RedisConstant.REDIS_HOST, ConstantUtil.RedisConstant.REDIS_PORT, ConstantUtil.RedisConstant.REDIS_PASSWORD);
        String data = RedisUtil.getData(phoneNumber);
        if (data.equals(number)) {
            return BaseUtil.success("验证码正确");
        } else {
            return BaseUtil.fail("验证码错误");
        }
    }

    @GetMapping("selectByPhone")
    public Result selectByPhone(String phone) {
        return BaseUtil.success(this.frontUserService.selectByPhone(phone));
    }

    @GetMapping("queryByIdUseToken")
    public Result queryByIdUseToken() {
        return BaseUtil.success(this.frontUserService.queryByIdUseToken());
    }

    @GetMapping("selectByToken")
    public Result selectByToken() {
        return BaseUtil.success(this.frontUserService.queryByIdUseToken());
    }

    /**
     * 获取每月注册用户统计数据
     */
    @GetMapping("/selectAllUserNumber")
    public Result<List<UserRegisterStatisticsVO>> selectAllUserNumber() {
        List<UserRegisterStatisticsVO> statistics = frontUserService.getUserRegisterStatistics();
        return BaseUtil.success(statistics);
    }

    /**
     * 获取总注册用户数
     */
    @GetMapping("/selectAllFrontUser")
    public Result<Integer> selectAllFrontUser() {
        int total = frontUserService.getTotalUserCount();
        return BaseUtil.success(total);
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return 查询结果
     */
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<FrontUser> page) {
        return BaseUtil.success(this.frontUserService.queryByPage(page));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public Result queryById(Integer id) {
        return BaseUtil.success(this.frontUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param frontUser 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody FrontUser frontUser) {
        return BaseUtil.success(this.frontUserService.insert(frontUser));
    }

    /**
     * 编辑数据
     *
     * @param frontUser 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public Result edit(@RequestBody FrontUser frontUser) {
        return BaseUtil.success(this.frontUserService.update(frontUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return BaseUtil.success(this.frontUserService.deleteById(id));
    }

}

